//
//  MYMyPublishVC.m
//  MuYing
//
//  Created by tbb on 2019/8/9.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYMyPublishVC.h"
#import "MYHomeAttentionContentCell.h"
#import "MYHomeAttentionTagCell.h"
#import "MYHomeAttentionToolCell.h"
#import "MYHomeAttentionImageCell.h"
#import "YQPHomeFollowModel.h"
#import "MYDynamicDetailsVC.h"
@interface MYMyPublishVC ()<MYBaseUserInfoCellDelegate>

@end

@implementation MYMyPublishVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isNavShadow = YES;
    self.navigationItem.title = kYQLS(@"我的发布");
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    kYQREGISTERTABLEVIEWCELL([MYHomeAttentionUserInfoCell class])
    kYQREGISTERTABLEVIEWNIBCELL(@"MYHomeAttentionContentCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYHomeAttentionTagCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYHomeAttentionImageCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYHomeAttentionToolCell")
    [self setupRefresh:self.listTableView];
    [self onStartRefresh];
}

-(void)onStartRefresh {
    self.page = 1;
    [self getPostList];
}

-(void)onStartLoadMore {
    self.page ++;
    [self getPostList];
}

-(void)getPostList {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"limit"] = @"10";
    parameters[@"page"] = @(self.page);
    
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendPostRequest:kPOST_USER_RELEASE_URL parameters:parameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        if (responseObject.code == 0) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            NSArray *arr = [YQPFollowModel mj_objectArrayWithKeyValuesArray:responseObject.data[@"list"]];
            [self.dataArr addObjectsFromArray:arr];
            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self stopRefresh];
        YQUITIPSERROR(error)
        YQHIDELOADINGTIPS
    }];
    
    
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    YQPFollowModel *model = nil;
    if (indexPath.section < self.dataArr.count) {
        model = self.dataArr[indexPath.section];
    }
    
    if (indexPath.row == 0) {
        MYHomeAttentionUserInfoCell *userCell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeAttentionUserInfoCell"];
        userCell.indexPath = indexPath;
        userCell.delegate = self;
        
        userCell.nameLb.text = model.name;
        [userCell.userIcon sd_setImageWithURL:[NSURL URLWithString:model.headImg] placeholderImage:IMAGE_NAMED(@"")];
        
        return userCell;
    }else if (indexPath.row == 1){
        MYHomeAttentionContentCell *contentCell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeAttentionContentCell"];
        contentCell.contentLb.text = model.title;
        return contentCell;
    }else if (indexPath.row == 2) {
        MYHomeAttentionTagCell *tagCell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeAttentionTagCell"];
        return tagCell;
    }else if(indexPath.row == 3) {
        MYHomeAttentionImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeAttentionImageCell"];
        //imageCell.bigImageView.image = YQIMG(@"Test_Rectangle_icon");
        imageCell.followModel = model;
        imageCell.carousel.autoScroll = NO;
        return imageCell;
    }else {
        MYHomeAttentionToolCell *toolCell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeAttentionToolCell"];
        return toolCell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 52;
    }else if (indexPath.row == 1) {
        return 42;
    }else if (indexPath.row == 2) {
        return 25;
    }else if (indexPath.row == 3) {
        return kScreenWidth;
    }
    return 42;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YQPFollowModel *model = nil;
    if (indexPath.section < self.dataArr.count) {
        model = self.dataArr[indexPath.section];
    }

    MYDynamicDetailsVC *detailsVC = [[MYDynamicDetailsVC alloc] init];
    detailsVC.channelId = model.channelId;
    detailsVC.invitationId = model.invitationId;
    kYQPUSH(detailsVC);
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 12;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
#pragma makr -- MYBaseUserInfoCellDelegate
- (void)moreActionIndexPath:(NSIndexPath *)indexPath {
    
    YQPFollowModel *model = nil;
    if (indexPath.section < self.dataArr.count) {
        model = self.dataArr[indexPath.section];
    }
    
    QMUIAlertController *alert = [[QMUIAlertController alloc] initWithTitle:@"" message:@"" preferredStyle:(QMUIAlertControllerStyleActionSheet)];
    
    
    [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"删除") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        [self deleteAction:model];
    }]];
    
//    [alert addAction:[QMUIAlertAction actionWithTitle:@"分享..." style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
//
//    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"取消") style:(QMUIAlertActionStyleCancel) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
    }]];
    alert.sheetButtonAttributes = @{NSForegroundColorAttributeName:kAPPThemeColor,NSFontAttributeName:kSystemMedium(20),NSKernAttributeName:@(0)};
    [alert showWithAnimated:YES];
    alert.sheetCancelButtonAttributes = @{NSForegroundColorAttributeName:kAPPThemeColor,NSFontAttributeName:kSystemMedium(20),NSKernAttributeName:@(0)};
    [alert showWithAnimated:YES];
}
#pragma makr -- MYBaseUserInfoCellDelegate end
-(void)deleteAction:(YQPFollowModel *)followModel {
    //取消订单
    QMUIAlertController *alert = [[QMUIAlertController alloc] initWithTitle:kYQLS(@"确定取消该订单?") message:@"" preferredStyle:(QMUIAlertControllerStyleAlert)];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"我再想一想") style:(QMUIAlertActionStyleCancel) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
    }]];
    alert.alertCancelButtonAttributes = @{NSForegroundColorAttributeName:kAPPThemeColor,NSFontAttributeName:UIFontBoldMake(17),NSKernAttributeName:@(0)};
    
    [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"确定") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"invitationId"] = followModel.invitationId;
        YQSHOWLOADINGTIPS(@"")
        [YQPNetWorkingManager sendPOSTWithForm:kPOST_INVITATION_REMOVE_URL parameters:parameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
            YQHIDELOADINGTIPS
            if (responseObject.code == 0) {
                YQUITIPSSUCCEED(responseObject.msg)
                [self getPostList];
            }else{
                YQUITIPSERROR(responseObject.msg);
            }
        } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
            YQUITIPSERROR(error);
            YQHIDELOADINGTIPS
        }];
        
    }]];
    alert.alertButtonAttributes = @{NSForegroundColorAttributeName:HexColor(0xB7B7B7),NSFontAttributeName:kSystemMedium(17),NSKernAttributeName:@(0)};
    
    [alert showWithAnimated:YES];
}
@end
